package com.example.service.impl;

import com.example.common.Const;
import com.example.common.ServiceResponse;
import com.example.mapper.LoginMapper;
import com.example.mapper.UserMapper;
import com.example.pojo.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.User;
import com.example.service.ILoginService;
import org.apache.shiro.crypto.hash.Md5Hash;
import util.JwtUtil;

/**
 * @BelongsProject: gateway-jwt-auth
 * @BelongsPackage: com.example.service.impl
 * @Author: LJ
 * @CreateTime: 2020-08-29 11:11
 * @Description:
 */
@Service
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private LoginMapper loginMapper;
    //盐
    private final String SALT = "huan";

    //散列次数
    private final Integer HASHITERATIONS = 5;

    @Override
    public ServiceResponse login(User user) {
        // shiro 对密码进行 MD5 加密
        Md5Hash md5 = new Md5Hash(user.getPassword(),SALT,HASHITERATIONS);
        user.setPassword(md5.toString());

        User login = loginMapper.login(user);

        if(login == null){
            return ServiceResponse.createError();
        }else{
            try {
                String token = JwtUtil.createToken(login);

                return ServiceResponse.createSuccess(token);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;


    }
}
